﻿using System;
using System.Text;

namespace ZYCommon
{
    

    public class StringBuffer
    {
        private int intCount = 0;
        private string[] vBuffer = new string[0x10];

        public void Append(string vData)
        {
            lock (this)
            {
                if (this.intCount >= this.vBuffer.Length)
                {
                    string[] strArray = new string[(int) (this.intCount * 1.5)];
                    for (int i = 0; i < this.vBuffer.Length; i++)
                    {
                        strArray[i] = this.vBuffer[i];
                    }
                    this.vBuffer = strArray;
                }
                this.vBuffer[this.intCount] = vData;
                this.intCount++;
            }
        }

        public void Clear()
        {
            lock (this)
            {
                this.vBuffer = new string[0x10];
                this.intCount = 0;
            }
        }

        public string[] ToArray()
        {
            string[] strArray = null;
            if (this.intCount > 0)
            {
                lock (this)
                {
                    strArray = new string[this.intCount];
                    for (int i = 0; i < this.intCount; i++)
                    {
                        strArray[i] = this.vBuffer[i];
                    }
                }
            }
            return strArray;
        }

        public string ToStringList(char SplitChar)
        {
            if (this.intCount > 0)
            {
                StringBuilder builder = new StringBuilder();
                lock (this)
                {
                    for (int i = 0; i < this.intCount; i++)
                    {
                        if (i != 0)
                        {
                            builder.Append(SplitChar);
                        }
                        builder.Append(this.vBuffer[i]);
                    }
                }
                return builder.ToString();
            }
            return null;
        }

        public int Count
        {
            get
            {
                return this.intCount;
            }
        }

        public string FirstItem
        {
            get
            {
                if (this.intCount > 0)
                {
                    return this.vBuffer[0];
                }
                return null;
            }
        }

        public string LastItem
        {
            get
            {
                if (this.intCount > 0)
                {
                    return this.vBuffer[this.intCount - 1];
                }
                return null;
            }
        }
    }
}

